<?php
/*IMPORTANT: 注意检查文件字符集是否为UTF-8*/
class api extends base
{
	public $apiversion;
	public function __construct($id=0)
	{
		parent::__construct();
		$this->apiversion = "0.1.0_r1";
	}
	public function __destruct()
	{
		parent::__destruct();
		unset($this->apiversion);
	}
}
class serverapi extends api
{
	public $ipaddrs;
	public $hostname;
	public $lastcheck;
	public $laststat;
	public $sn;
	public $info;
	protected $roleid;
	protected $uuid;
	protected $enabled;
	public function __construct($id=0)
	{
		parent::__construct();
		if($id>0)
		{
			$this->Load($id);
		}
	}
	public function __destruct()
	{
		parent::__destruct();
		unset($this->ipaddrs);
		unset($this->hostname);
		unset($this->lastcheck);
		unset($this->laststat);
		unset($this->sn);
		unset($this->info);
		unset($this->roleid);
		unset($this->uuid);
		unset($this->enabled);
	}
	//return uuid or reg new server;
	public function GetUuid($hostname,$sn,$ipaddr)
	{
		$db = new db;
		$server = $db->GetWithLike('Server',array('hostname'=>$hostname,'sn'=>$sn),array('ipaddrs'=>$ipaddr));
		//$server = $db->Get('Server',array('hostname'=>$hostname,'sn'=>$sn));
		if ($server)
		{
			$db->Set('Server',array('uuid'=>$server['uuid']),array('lastcheck'=>date("Y-m-d H:i:s"),'laststat'=>'Handshake'));
			return $server['uuid'];
		}
		else
		{
			$uuid = $this->RegNewServer($hostname,$sn,$ipaddr);
			return $uuid;
		}
	}
	public function LoginByUuid($uuid)
	{
		;
	}
	private function RegNewServer($hostname,$sn,$ipaddrs)
	{
		$uuid = UtilFuncGenerateUuid();
		$db = new db;
		$server = $db->Ins('Server',array("hostname"=>$hostname,"ipaddrs"=>$ipaddrs,"sn"=>$sn,"laststat"=>"New","roleid"=>1,"uuid"=>$uuid,"enabled"=>1));
		if ($server)
		{
			return $uuid;
		}
		return false;
	}
	public function Load($id)
	{
		;
	}
	public function LoadConvert($id)
	{
		$db = new db;
		$server = $db->Get('Server',$id);
	}
}
?>